﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using DoAnWeb.DTO;

namespace DoAnWeb.DAO
{
    public class TinKhuyenMaiDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<TinKhuyenMaiDTO> GetList()
        {
            if (dp.OpenConnect())
            {
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_TinKhuyenMai", null);
                dp.CloseConnect();
                BindingList<TinKhuyenMaiDTO> List = new BindingList<TinKhuyenMaiDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
                    DTO.MaTinKhuyenMai = int.Parse(row["MaTinKhuyenMai"].ToString());
                    DTO.TieuDe = row["TieuDe"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.MoTa = row["MoTa"].ToString();
                    DTO.TenTaiKhoanDang = row["TenTaiKhoanDang"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.PhanTramKhuyenMai = int.Parse(row["PhanTramKhuyenMai"].ToString());
                    DTO.NgayDang = DateTime.Parse(row["NgayDang"].ToString());
                    DTO.NgayHetHan = DateTime.Parse(row["NgayHetHan"].ToString());
                    if (int.Parse(row["DaDuyet"].ToString()) == 1)
                    {
                        DTO.DaDuyet = "Đã duyệt";
                    }
                    else
                    {
                        DTO.DaDuyet = "Chưa duyệt";
                    }
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public BindingList<TinKhuyenMaiDTO> GetList_New()
        {
            if (dp.OpenConnect())
            {

                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_TinKhuyenMai_New", null);
                dp.CloseConnect();
                BindingList<TinKhuyenMaiDTO> List = new BindingList<TinKhuyenMaiDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
                    DTO.MaTinKhuyenMai = int.Parse(row["MaTinKhuyenMai"].ToString());
                    DTO.TieuDe = row["TieuDe"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.MoTa = row["MoTa"].ToString();
                    DTO.TenTaiKhoanDang = row["TenTaiKhoanDang"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.PhanTramKhuyenMai = int.Parse(row["PhanTramKhuyenMai"].ToString());
                    DTO.NgayDang = DateTime.Parse(row["NgayDang"].ToString());
                    DTO.NgayHetHan = DateTime.Parse(row["NgayHetHan"].ToString());
                    if (int.Parse(row["DaDuyet"].ToString()) == 1)
                    {
                        DTO.DaDuyet = "Đã duyệt";
                    }
                    else
                    {
                        DTO.DaDuyet = "Chưa duyệt";
                    }
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public BindingList<TinKhuyenMaiDTO> GetList_ChuaXoa()
        {
            if (dp.OpenConnect())
            {

                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_TinKhuyenMai_ChuaXoa", null);
                dp.CloseConnect();
                BindingList<TinKhuyenMaiDTO> List = new BindingList<TinKhuyenMaiDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
                    DTO.MaTinKhuyenMai = int.Parse(row["MaTinKhuyenMai"].ToString());
                    DTO.TieuDe = row["TieuDe"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.MoTa = row["MoTa"].ToString();
                    DTO.TenTaiKhoanDang = row["TenTaiKhoanDang"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.PhanTramKhuyenMai = int.Parse(row["PhanTramKhuyenMai"].ToString());
                    DTO.NgayDang = DateTime.Parse(row["NgayDang"].ToString());
                    DTO.NgayHetHan = DateTime.Parse(row["NgayHetHan"].ToString());
                    if (int.Parse(row["DaDuyet"].ToString()) == 1)
                    {
                        DTO.DaDuyet = "Đã duyệt";
                    }
                    else
                    {
                        DTO.DaDuyet = "Chưa duyệt";
                    }
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public BindingList<TinKhuyenMaiDTO> GetList_TenTaiKhoan(string _TenTaiKhoan)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@TenTaiKhoan", _TenTaiKhoan) };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_TinKhuyenMai_TenTaiKhoan", sqlParameter);
                dp.CloseConnect();
                BindingList<TinKhuyenMaiDTO> List = new BindingList<TinKhuyenMaiDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
                    DTO.MaTinKhuyenMai = int.Parse(row["MaTinKhuyenMai"].ToString());
                    DTO.TieuDe = row["TieuDe"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.MoTa = row["MoTa"].ToString();
                    DTO.TenTaiKhoanDang = row["TenTaiKhoanDang"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.PhanTramKhuyenMai = int.Parse(row["PhanTramKhuyenMai"].ToString());
                    DTO.NgayDang = DateTime.Parse(row["NgayDang"].ToString());
                    DTO.NgayHetHan = DateTime.Parse(row["NgayHetHan"].ToString());
                    if (int.Parse(row["DaDuyet"].ToString()) == 1)
                    {
                        DTO.DaDuyet = "Đã duyệt";
                    }
                    else
                    {
                        DTO.DaDuyet = "Chưa duyệt";
                    }
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public BindingList<TinKhuyenMaiDTO> GetList_TieuDe(string _TieuDe)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@TieuDe", _TieuDe) };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_TinKhuyenMai_TieuDe", sqlParameter);
                dp.CloseConnect();
                BindingList<TinKhuyenMaiDTO> List = new BindingList<TinKhuyenMaiDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
                    DTO.MaTinKhuyenMai = int.Parse(row["MaTinKhuyenMai"].ToString());
                    DTO.TieuDe = row["TieuDe"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.MoTa = row["MoTa"].ToString();
                    DTO.TenTaiKhoanDang = row["TenTaiKhoanDang"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.PhanTramKhuyenMai = int.Parse(row["PhanTramKhuyenMai"].ToString());
                    DTO.NgayDang = DateTime.Parse(row["NgayDang"].ToString());
                    DTO.NgayHetHan = DateTime.Parse(row["NgayHetHan"].ToString());
                    if (int.Parse(row["DaDuyet"].ToString()) == 1)
                    {
                        DTO.DaDuyet = "Đã duyệt";
                    }
                    else
                    {
                        DTO.DaDuyet = "Chưa duyệt";
                    }
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public BindingList<TinKhuyenMaiDTO> GetList_Top()
        {
            if (dp.OpenConnect())
            {
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_TinKhuyenMai_Top", null);
                dp.CloseConnect();
                BindingList<TinKhuyenMaiDTO> List = new BindingList<TinKhuyenMaiDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
                    DTO.MaTinKhuyenMai = int.Parse(row["MaTinKhuyenMai"].ToString());
                    DTO.TieuDe = row["TieuDe"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.MoTa = row["MoTa"].ToString();
                    DTO.TenTaiKhoanDang = row["TenTaiKhoanDang"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.PhanTramKhuyenMai = int.Parse(row["PhanTramKhuyenMai"].ToString());
                    DTO.NgayDang = DateTime.Parse(row["NgayDang"].ToString());
                    DTO.NgayHetHan = DateTime.Parse(row["NgayHetHan"].ToString());
                    if (int.Parse(row["DaDuyet"].ToString()) == 1)
                    {
                        DTO.DaDuyet = "Đã duyệt";
                    }
                    else
                    {
                        DTO.DaDuyet = "Chưa duyệt";
                    }
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public BindingList<TinKhuyenMaiDTO> GetList_ForKind(int _MaLoaiSanPham)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaLoaiSanPham", _MaLoaiSanPham) };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_TinKhuyenMai_ForKind", sqlParameter);
                dp.CloseConnect();
                BindingList<TinKhuyenMaiDTO> List = new BindingList<TinKhuyenMaiDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
                    DTO.MaTinKhuyenMai = int.Parse(row["MaTinKhuyenMai"].ToString());
                    DTO.TieuDe = row["TieuDe"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.MoTa = row["MoTa"].ToString();
                    DTO.TenTaiKhoanDang = row["TenTaiKhoanDang"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.PhanTramKhuyenMai = int.Parse(row["PhanTramKhuyenMai"].ToString());
                    DTO.NgayDang = DateTime.Parse(row["NgayDang"].ToString());
                    DTO.NgayHetHan = DateTime.Parse(row["NgayHetHan"].ToString());
                    if (int.Parse(row["DaDuyet"].ToString()) == 1)
                    {
                        DTO.DaDuyet = "Đã duyệt";
                    }
                    else
                    {
                        DTO.DaDuyet = "Chưa duyệt";
                    }
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public TinKhuyenMaiDTO GetFromId(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaTinKhuyenMai", _Ma) };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetFromID_TinKhuyenMai", sqlParameter);
                dp.CloseConnect();
                TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
                foreach (DataRow row in dt.Rows)
                {
                    DTO.MaTinKhuyenMai = int.Parse(row["MaTinKhuyenMai"].ToString());
                    DTO.TieuDe = row["TieuDe"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.MoTa = row["MoTa"].ToString();
                    DTO.TenTaiKhoanDang = row["TenTaiKhoanDang"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());
                    DTO.PhanTramKhuyenMai = int.Parse(row["PhanTramKhuyenMai"].ToString());
                    DTO.NgayDang = DateTime.Parse(row["NgayDang"].ToString());
                    DTO.NgayHetHan = DateTime.Parse(row["NgayHetHan"].ToString());
 					DTO.MaLoaiSanPham = int.Parse(row["MaLoaiSanPham"].ToString());                    DTO.TenLoaiSanPham = row["TenLoaiSanPham"].ToString();
                    if (int.Parse(row["DaDuyet"].ToString()) == 1)
                    {
                        DTO.DaDuyet = "Đã duyệt";
                    }
                    else
                    {
                        DTO.DaDuyet = "Chưa duyệt";
                    }
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                }
                return DTO;
            }
            return null;
        }

        public bool Duyet(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaTinKhuyenMai", _Ma) };
                bool kq = dp.SqlExcuteNonQuery("uproc_DuyetAdmin", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaTinKhuyenMai", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_TinKhuyenMai", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool CheckDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaTinKhuyenMai", _Ma) };
                bool kq = dp.SqlExcuteNonQuery("uproc_CheckDelete_TinKhuyenMai", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UnCheckDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaTinKhuyenMai", _Ma) };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnCheckDelete_TinKhuyenMai", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(TinKhuyenMaiDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@DiaChi", _DTO.DiaChi), 
                    new SqlParameter("@DienThoai", _DTO.DienThoai), 
                    new SqlParameter("@GiaBan", _DTO.GiaBan), 
                    new SqlParameter("@GiaGoc", _DTO.GiaGoc),
                    new SqlParameter("@HinhAnh", _DTO.HinhAnh),
                    new SqlParameter("@MaLoaiSanPham", _DTO.MaLoaiSanPham),
                    new SqlParameter("@MoTa", _DTO.MoTa),
                    new SqlParameter("@NgayDang", _DTO.NgayDang),
                    new SqlParameter("@NgayHetHan", _DTO.NgayHetHan),
                    new SqlParameter("@PhanTramKhuyenMai", _DTO.PhanTramKhuyenMai),
                    new SqlParameter("@TenTaiKhoanDang", _DTO.TenTaiKhoanDang) ,
                    new SqlParameter("@TieuDe", _DTO.TieuDe)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_TinKhuyenMai", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }

        public bool Update(TinKhuyenMaiDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaTinKhuyenMai", _DTO.MaTinKhuyenMai), 
                    new SqlParameter("@DiaChi", _DTO.DiaChi), 
                    new SqlParameter("@DienThoai", _DTO.DienThoai), 
                    new SqlParameter("@GiaBan", _DTO.GiaBan), 
                    new SqlParameter("@GiaGoc", _DTO.GiaGoc),
                    new SqlParameter("@HinhAnh", _DTO.HinhAnh),
                    new SqlParameter("@MaLoaiSanPham", _DTO.MaLoaiSanPham),
                    new SqlParameter("@MoTa", _DTO.MoTa),
                    new SqlParameter("@NgayDang", _DTO.NgayDang),
                    new SqlParameter("@NgayHetHan", _DTO.NgayHetHan),
                    new SqlParameter("@PhanTramKhuyenMai", _DTO.PhanTramKhuyenMai),
                    new SqlParameter("@TenTaiKhoanDang", _DTO.TenTaiKhoanDang) ,
                    new SqlParameter("@TieuDe", _DTO.TieuDe)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Update_TinKhuyenMai", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }

        public int CountNumberRemain(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaTinKhuyenMai", _Ma) };
                object kq = dp.SqlExcuteScalar("uproc_CountNumberRemain_TinKhuyenMai", sqlParameter);
                dp.CloseConnect();
                return Convert.ToInt32(kq);
            }
            return -1;
        }

        public int CountNumberSelled(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaTinKhuyenMai", _Ma) };
                object kq = dp.SqlExcuteScalar("uproc_CountNumberSell_TinKhuyenMai", sqlParameter);
                dp.CloseConnect();
                return Convert.ToInt32(kq);
            }
            return -1;
        }

        public int GetCount(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaTinKhuyenMai", _Ma) };
                object kq = dp.SqlExcuteScalar("uproc_GetCount_TinKhuyenMai", sqlParameter);
                dp.CloseConnect();
                return Convert.ToInt32(kq);
            }
            return -1;
        }

    }
}